<!DOCTYPE html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Unity WebGL Player | New Unity Project</title>
    <link rel="shortcut icon" href="TemplateData/favicon.ico">
    <link rel="stylesheet" href="TemplateData/style.css">
    
    <!-- 透明背景样式 -->
    <style>
      /* 重置默认样式并设置透明背景 */
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      
      body {
        background: transparent !important;
        overflow: hidden;
        width: 100vw;
        height: 100vh;
        font-family: Arial, sans-serif;
      }
      
      /* Unity容器透明 */
      #unity-container {
        position: relative;
        width: 100%;
        height: 100%;
        background: transparent !important;
      }
      
      /* Canvas透明 - 关键设置 */
      #unity-canvas {
        width: 100%;
        height: 100%;
        background: transparent !important;
        display: block;
      }
      
      /* 确保UI元素不会阻挡透明区域 */
      #unity-loading-bar {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        background: transparent;
      }
      
      #unity-footer {
        background: transparent;
      }
      
      /* 移动端适配 */
      .unity-mobile #unity-canvas {
        width: 100%;
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="unity-container" class="unity-desktop">
      <canvas id="unity-canvas" width=1920 height=920 tabindex="-1"></canvas>
      <div id="unity-loading-bar">
        <div id="unity-logo"></div>
        <div id="unity-progress-bar-empty">
          <div id="unity-progress-bar-full"></div>
        </div>
      </div>
      <div id="unity-warning"> </div>
      <div id="unity-footer">
        <div id="unity-webgl-logo"></div>
        <div id="unity-fullscreen-button"></div>
        <div id="unity-build-title">New Unity Project</div>
      </div>
    </div>
    <script>
      // 获取DOM元素
      var container = document.querySelector("#unity-container");
      var canvas = document.querySelector("#unity-canvas");
      var loadingBar = document.querySelector("#unity-loading-bar");
      var progressBarFull = document.querySelector("#unity-progress-bar-full");
      var fullscreenButton = document.querySelector("#unity-fullscreen-button");
      var warningBanner = document.querySelector("#unity-warning");

      // 显示警告/错误信息的函数
      function unityShowBanner(msg, type) {
        function updateBannerVisibility() {
          warningBanner.style.display = warningBanner.children.length ? 'block' : 'none';
        }
        var div = document.createElement('div');
        div.innerHTML = msg;
        warningBanner.appendChild(div);
        if (type == 'error') div.style = 'background: red; padding: 10px;';
        else {
          if (type == 'warning') div.style = 'background: yellow; padding: 10px;';
          setTimeout(function() {
            warningBanner.removeChild(div);
            updateBannerVisibility();
          }, 5000);
        }
        updateBannerVisibility();
      }

      // 构建配置
      var buildUrl = "Build";
      var loaderUrl = buildUrl + "/walk.loader.js";
      var config = {
        dataUrl: buildUrl + "/walk.data.unityweb",
        frameworkUrl: buildUrl + "/walk.framework.js.unityweb",
        codeUrl: buildUrl + "/walk.wasm.unityweb",
        streamingAssetsUrl: "StreamingAssets",
        companyName: "DefaultCompany",
        productName: "New Unity Project",
        productVersion: "1.0",
        showBanner: unityShowBanner,
      };

      // 移动端检测和适配
      if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
        var meta = document.createElement('meta');
        meta.name = 'viewport';
        meta.content = 'width=device-width, height=device-height, initial-scale=1.0, user-scalable=no, shrink-to-fit=yes';
        document.getElementsByTagName('head')[0].appendChild(meta);
        container.className = "unity-mobile";
        canvas.className = "unity-mobile";
      } else {
        // 桌面端设置固定尺寸
        canvas.style.width = "1920px";
        canvas.style.height = "920px";
      }

      // 显示加载条
      loadingBar.style.display = "block";

      // 加载并初始化Unity
      var script = document.createElement("script");
      script.src = loaderUrl;
      script.onload = () => {
        createUnityInstance(canvas, config, (progress) => {
          progressBarFull.style.width = 100 * progress + "%";
        }).then((unityInstance) => {
          loadingBar.style.display = "none";
          fullscreenButton.onclick = () => {
            unityInstance.SetFullscreen(1);
          };
          
          // 额外的透明背景确保
          setTimeout(() => {
            canvas.style.backgroundColor = 'transparent';
          }, 100);
          
        }).catch((message) => {
          alert(message);
        });
      };

      document.body.appendChild(script);

      // 窗口调整大小时保持透明
      window.addEventListener('resize', function() {
        canvas.style.backgroundColor = 'transparent';
      });
    </script>
  </body>
</html>

